package j27;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author chengshuai
 * @date 2021/4/13 9:41
 * <p>
 * 测试lock可重入锁
 */
public class Test2 {
    public Lock lock = new ReentrantLock();
    public void f1() {
        lock.lock();
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getId() + "====》f1");
            f2();
        } finally {
            lock.unlock();
            lock.unlock();
        }
    }
    public void f2() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getId() + "====》f2");
        } finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        Test2 test2 = new Test2();
        new Thread(() -> {
            test2.f1();
        }).start();
        new Thread(() -> {
            test2.f1();
        }).start();
    }
}
